package com.lifeCode.boot.leetcode;

import java.util.HashMap;
import java.util.HashSet;

/**
 * @Author: FanGang
 * @Date: 2023/3/3 9:48
 */
public class LeetCodeTest1 {
    //https://leetcode.cn/problems/making-file-names-unique/ 保证文件名唯一

    public String[] getFolderNames(String[] names) {
        String[] res = new String[names.length];
        HashMap<String, Integer> keyMap = new HashMap<>();
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            //不存在
            if (!keyMap.containsKey(name)) {
                res[i] = name;
                keyMap.put(name, 1);
            } else {
                int ind = keyMap.get(name);
                //存在判断后缀添加是否还存在
                while (keyMap.containsKey(addSuffix(name, ind))) {
                    ind++;
                }
                //确定不存在的后缀
                res[i] = addSuffix(name, ind);
                keyMap.put(name, ind + 1);
                keyMap.put(addSuffix(name, ind), 1);
            }
        }
        return res;
    }

    //给定行和列的和求可行矩阵  https://leetcode.cn/problems/find-valid-matrix-given-row-and-column-sums/
    public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
        int[][] res = new int[rowSum.length][colSum.length];
        int i = 0, j = 0;
        while (i < rowSum.length && j < colSum.length) {
            int v = Math.min(rowSum[i], colSum[j]);
            res[i][j] = v;
            rowSum[i] -= v;
            colSum[j] -= v;
            if (rowSum[i] == 0) {
                i++;
            }
            if (colSum[j] == 0) {
                j++;
            }
        }
        return res;
    }

    private String addSuffix(String name, int ind) {
        return name + "(" + ind + ")";
    }


    public static void main(String[] args) {
        LeetCodeTest1 leetCodeTest1 = new LeetCodeTest1();
    }
}
